package nl.txproductions.twitterclient.model;

import java.lang.reflect.Type;
import java.util.List;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;


public class JsonParser {

    private Gson gson;

    public JsonParser() {
        setGson();
    }

    private void setGson() {
        GsonBuilder gBuilder = new GsonBuilder();
        gBuilder.registerTypeAdapter(Message.class, new JsonDeserializer<Message>() {

            @Override
            public Message deserialize(JsonElement json, Type typeOfT,
                    JsonDeserializationContext context)
            throws JsonParseException {

                Message message = new Message();
                JsonObject data = json.getAsJsonObject();

                JsonElement element = null;

                element = data.get("in_reply_to_screen_name"); 
                if(!element.isJsonNull()) {
                    message.setInReplyToName(element.getAsString());
                }
                element = data.get("in_reply_to_user_id");
                if(!element.isJsonNull()) {
                    message.setInReplyToId(element.getAsString());
                }
                element = data.get("text");
                if(!element.isJsonNull()) {
                    message.setText(element.getAsString());
                }
                element = data.get("in_reply_to_screen_name");
                if(!element.isJsonNull()) {
                    message.setInReplyToName(element.getAsString());
                }
                element = data.get("source");
                if(!element.isJsonNull()) {
                    message.setSource(element.getAsString());
                }
                element = data.get("created_at");
                if(!element.isJsonNull()) {
                    message.setCreatedAt(element.getAsString());
                }
                element = data.get("id");
                if(!element.isJsonNull()) {
                    message.setId(element.getAsInt());
                }
                message.setUser(gson.fromJson(data.get("user"), User.class));

                return message;
            }
        });

        gBuilder.registerTypeAdapter(User.class, new JsonDeserializer<User>() {

            @Override
            public User deserialize(JsonElement json, Type typeOfT,
                    JsonDeserializationContext context)
            throws JsonParseException {

                User user = new User();
                JsonObject data = json.getAsJsonObject();

                JsonElement element = null;
                
                element = data.get("description");
                if(!element.isJsonNull()) {
                    user.setDescription(element.getAsString());
                }
                element = data.get("screen_name");
                if(!element.isJsonNull()) {
                    user.setScreenName(element.getAsString());
                }
                element = data.get("name");
                if(!element.isJsonNull()) {
                    user.setName(element.getAsString());
                }
                element = data.get("id");
                if(!element.isJsonNull()) {
                    user.setId(element.getAsInt());
                }

                return user;
            }
        });

        this.gson = gBuilder.create();
    }

    public List<Message> getMessagesFromJson(String json) {
        Type type = new TypeToken<List<Message>>() {}.getType();
        return gson.fromJson(json, type);
    }
}
